package com.yjt.fresh.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvAtyProducts;
import com.yjt.fresh.api.ApiGetProducts;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanProduct;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.service.Utils;
import com.yjt.fresh.utils.CommonsToolsHelper;
import com.yjt.fresh.utils.PreferenceManager;
import com.yjt.fresh.widget.GlideCircleTransform;
import com.yjt.fresh.widget.GridSpacingItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 店铺详情
 * Created by mcc on 2018/7/23.
 */

public class AtyStoreDetail extends BaseSimpleActivity {
    private static final int PAGE_SIZE = 20;
    @BindView(R.id.rl_app_aty_category_back)
    RelativeLayout mRlBack;
    @BindView(R.id.tv_app_aty_category_search)
    TextView mTvSearch;
    @BindView(R.id.ivAppAtyStoreDetailCategory)
    ImageView mIvCategory;
    @BindView(R.id.ivAppAtyStoreDetailLogo)
    ImageView mIvLogo;
    @BindView(R.id.tvAppAtyStoreDetailName)
    TextView mTvName;
    @BindView(R.id.tvAppAtyStoreDetailDistance)
    TextView mTvDistance;
    @BindView(R.id.tvAppAtyStoreDetailHot)
    TextView mTvHot;
    @BindView(R.id.rlAppAtyStoreDetailHot)
    RelativeLayout mRlHot;
    @BindView(R.id.tvAppAtyStoreDetailSpecial)
    TextView mTvSpecial;
    @BindView(R.id.rlAppAtyStoreDetailSpecial)
    RelativeLayout mRlSpecial;
    @BindView(R.id.tvAppAtyStoreDetailPriceCut)
    TextView mTvPriceCut;
    @BindView(R.id.rlAppAtyStoreDetailPriceCut)
    RelativeLayout mRlPriceCut;
    @BindView(R.id.rvAppAtyStoreDetail)
    RecyclerView mRv;
    @BindView(R.id.tvAppAtyStoreDetailService)
    TextView mTvService;
    @BindView(R.id.tvAppAtyStoreDetailCategory)
    TextView mTvCategory;
    @BindView(R.id.tvAppAtyStoreDetailGetCoupon)
    TextView mTvGetCoupon;
    @BindView(R.id.srlAppAtyStoreDetail)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.vAppAtyStoreDetailHot)
    View mVHot;
    @BindView(R.id.vAppAtyStoreDetailSpecial)
    View mVSpecial;
    @BindView(R.id.vAppAtyStoreDetailPriceCut)
    View mVCut;
    private final int HOT = 0;
    private final int SPECIAL = 1;
    private final int PRICE_CUT = 2;
    private int mNextRequestPage = 1;
    private boolean isLoadMore;
    private AdpRvAtyProducts mAdpProducts;
    private boolean isShowProgress;
    private Map<String, String> mParamMap;
    private String mShopType = HOT+"";
    private String mStoreID;
    private String mStorePhone;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_store_detail;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        String storeName = getIntent().getStringExtra(MyConstants.STORE_NAME);
        String storeLogo = getIntent().getStringExtra(MyConstants.STORE_LOGO);
        String storeLocation = getIntent().getStringExtra(MyConstants.STORE_LOCATION);
        mStorePhone = getIntent().getStringExtra(MyConstants.STORE_PHONE);
        mStoreID = getIntent().getStringExtra(MyConstants.STORE_ID);
        String[] location = storeLocation.split(",");
        double storeLatitude = Double.valueOf(PreferenceManager.getInstance().getLatitude());
        double storeLongitude = Double.valueOf(PreferenceManager.getInstance().getLongitude());
        double distance = Utils.getDistance(storeLongitude, storeLatitude, Double.valueOf(location[0]), Double.valueOf(location[1]));
        mTvDistance.setText(distance+"km");
        mTvName.setText(storeName);

        Glide.with(mContext).load(storeLogo).bitmapTransform(new GlideCircleTransform(mContext)).into(mIvLogo);
    }

    @Override
    protected void initEvent() {
        initRv();
        getData();
        initClick();
        initRefresh();
    }


    private void initRv() {
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2);
        List<BeanProduct> list = new ArrayList<>();
        mRv.setLayoutManager(layoutManager);
        mAdpProducts = new AdpRvAtyProducts(R.layout.app_recycle_item_products, list);
        mRv.addItemDecoration(new GridSpacingItemDecoration(2, CommonsToolsHelper.dip2px(mContext, 5), true));
        mRv.setAdapter(mAdpProducts);
    }

    HttpOnNextListener<List<BeanProduct>> mProductsListener = new HttpOnNextListener<List<BeanProduct>>() {
        @Override
        public void onNext(List<BeanProduct> beanProducts) {
            if (isLoadMore) {
                setData(false, beanProducts);
            } else {
                setData(true, beanProducts);
                mRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void setData(boolean isRefresh, List<BeanProduct> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdpProducts.setNewData(data);
        } else {
            if (size > 0) {
                mAdpProducts.addData(data);
                mAdpProducts.loadMoreComplete();
            }else {
                mAdpProducts.loadMoreEnd(true);
            }
        }
    }

    private void getData() {
        mParamMap = new HashMap<>();
        mParamMap.put("currentPage", mNextRequestPage+"");
        mParamMap.put("STORE_ID", getIntent().getStringExtra(MyConstants.STORE_ID));
        mParamMap.put("INSHOPTYPE", mShopType);

        ApiGetProducts apiGetProducts = new ApiGetProducts(mProductsListener, mContext, true, false, true);
        apiGetProducts.setMap(mParamMap);
        HttpManager.getInstance().doHttpDeal(apiGetProducts);
    }

    private void initClick() {
        mAdpProducts.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext, AtyProductDetail.class);
                intent.putExtra(MyConstants.GOODS_ID,mAdpProducts.getItem(position).getGOODS_ID());
                startActivity(intent);
            }
        });
        mRlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mTvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, AtyStoreSearch.class);
                intent.putExtra(MyConstants.STORE_ID,getIntent().getStringExtra(MyConstants.STORE_ID));
                startActivity(intent);
            }
        });

        mRlHot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvHot.setTextColor(getResources().getColor(R.color.color_43b624));
                mVHot.setVisibility(View.VISIBLE);

                mTvSpecial.setTextColor(getResources().getColor(R.color.color_333333));
                mVSpecial.setVisibility(View.GONE);
                mTvPriceCut.setTextColor(getResources().getColor(R.color.color_333333));
                mVCut.setVisibility(View.GONE);

                mNextRequestPage = 1;
                isLoadMore = false;
                mShopType = HOT+"";
                getData();
            }
        });

        mRlSpecial.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvSpecial.setTextColor(getResources().getColor(R.color.color_43b624));
                mVSpecial.setVisibility(View.VISIBLE);

                mTvHot.setTextColor(getResources().getColor(R.color.color_333333));
                mVHot.setVisibility(View.GONE);
                mTvPriceCut.setTextColor(getResources().getColor(R.color.color_333333));
                mVCut.setVisibility(View.GONE);

                mNextRequestPage = 1;
                isLoadMore = false;
                mShopType = SPECIAL + "";
                getData();
            }
        });

        mRlPriceCut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvPriceCut.setTextColor(getResources().getColor(R.color.color_43b624));
                mVCut.setVisibility(View.VISIBLE);

                mTvHot.setTextColor(getResources().getColor(R.color.color_333333));
                mVHot.setVisibility(View.GONE);
                mTvSpecial.setTextColor(getResources().getColor(R.color.color_333333));
                mVSpecial.setVisibility(View.GONE);

                mNextRequestPage = 1;
                isLoadMore = false;
                mShopType = PRICE_CUT+"";
                getData();
            }
        });

        mTvService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginDialog();
            }
        });

        mTvCategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,AtyProductCategory.class);
                intent.putExtra(MyConstants.FROM_WHERE,MyConstants.STORE);
                intent.putExtra(MyConstants.STORE_ID,mStoreID);
                startActivity(intent);
            }
        });

        mIvCategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,AtyProductCategory.class);
                intent.putExtra(MyConstants.FROM_WHERE,MyConstants.STORE);
                intent.putExtra(MyConstants.STORE_ID,mStoreID);
                startActivity(intent);
            }
        });
        mTvGetCoupon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PreferenceManager.getInstance().getIsLogin()) {
                    Intent intent = new Intent(mContext,AtyGetCoupon.class);
                    intent.putExtra(MyConstants.STORE_ID,getIntent().getStringExtra(MyConstants.STORE_ID));
                    startActivity(intent);
                }else {
                    toLogin();
                }
            }
        });
    }

    private void initRefresh() {
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mNextRequestPage = 1;
                isLoadMore = false;
                getData();
            }
        });
        mAdpProducts.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                isLoadMore = true;
                getData();
            }
        }, mRv);
        mAdpProducts.setEmptyView(R.layout.view_empty);
    }

    private void loginDialog() {
        final Dialog dialog = new Dialog(mContext, R.style.MyDialog);
        dialog.setContentView(R.layout.app_dialog_sure_cancel);
        TextView tvContent = dialog.findViewById(R.id.tvAppDIalogSureCancelContent);
        TextView tvTitle = dialog.findViewById(R.id.tvAppDIalogSureCancelTitle);
        tvTitle.setVisibility(View.VISIBLE);
        TextView tvCancel = dialog.findViewById(R.id.tvAppDialogSureCancelCancel);
        TextView tvSure = dialog.findViewById(R.id.tvAppDialogSureCancelSure);
        tvTitle.setText("即将拨通客服电话哦");
        tvContent.setText(mStorePhone);
        tvCancel.setText("取消");
        tvSure.setText("立即拨打");
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMobileDialerInterface(mStorePhone);
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        dialog.show();
    }
}
